/**
 * Created by wry on 2015/5/14.
 */

var bannerService = require('../../services/backoffice/backOffice-bannerService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');

exports.addCommunityBanner = function (req, res) {

    var input = req.body;

    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
        } else {
            var data = [];
            images.forEach(function (image) {
                if (image != null) {
                    data.push(image);
                }
            });
            if (data.length > 0)
                input.imageURL = data[0];

            logger.info(input);

            bannerService.addCommunityBanner(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when addCommunityBanner ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};

exports.updateCommunityBanner = function (req, res) {
    var input = req.body;
    toolUtil.uploadImageService(null, null, req, function (err, images) {
        if (err) {
            res.json(messageUtil.composeFailedMessage(req, 'Failed to upload Images... - ' + err.message));
        } else {
            var data = [];
            images.forEach(function (image) {
                if (image != null) {
                    data.push(image);
                }
            });
            if (data.length > 0)
                input.imageURL = data[0];

            bannerService.updateCommunityBanner(input, function (err, result) {
                if (err) {
                    logger.error('Exception happened when updateCommunityBanner ...!' + err.message);
                    res.json(messageUtil.composeFailedMessage(req, err.message));
                } else {
                    res.json(messageUtil.composeSucceededMessage(req, null, null));
                }
            })
        }
    });
};

exports.getAllContentListByCommunityId = function (req, res) {
    var input = req.body;

    bannerService.getAllContentListByCommunityIdService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when getAllContentListByCommunityId ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                question: result[0],
                activity: result[1],
                topic: result[2],
                notice: result[3],
                lottery: result[4],
                iphone: result[5]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.getBannerList = function (req, res) {
    var page = req.params.page;
    var type = req.params.type;

    bannerService.getBannerListService(page, type, function (err, result) {
        if (err) {
            logger.error('Exception happened when getBannerList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.getBannerDetail = function (req, res) {
    var bannerId = req.params.bannerId;

    bannerService.getBannerDetailService(bannerId, function (err, result) {
        if (err) {
            logger.error('Exception happened when getBannerDetail ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            var data = {
                banner: result[0],
                content: result[1],
                bindIds: result[2].bindIds ? result[2].bindIds : '',
                bindList: result[2].bindList
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    })
};

exports.deleteBannerBind = function (req, res) {
    var bindId = req.params.bindId;

    bannerService.deleteBannerBindService(bindId, function (err, result) {
        if (err) {
            logger.error('Exception happened when deleteBannerBind ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.bindCommunityBanner = function (req, res) {
    var input = req.body;

    bannerService.bindCommunityBannerService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when bindCommunityBanner ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
};

exports.getCommunityBannerList = function (req, res) {
    var communityId = req.params.communityId;
    var type = req.params.type;

    bannerService.getCommunityBannerListService(communityId, type, function (err, result) {
        if (err) {
            logger.error('Exception happened when getCommunityBannerList ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, result));
        }
    })
};

exports.setBindBannerOrder = function (req, res) {
    var input = req.body;

    bannerService.setBindBannerOrderService(input, function (err, result) {
        if (err) {
            logger.error('Exception happened when setBindBannerOrder ...!' + err.message);
            res.json(messageUtil.composeFailedMessage(req, err.message));
        } else {
            res.json(messageUtil.composeSucceededMessage(req, null, null));
        }
    })
}